﻿using System;
using System.Windows.Forms;
using EFCoreRepository.Extensions;
using Microsoft.Data.SqlClient;
using RescueApp.Data;
using RescueApp.Models;

namespace RescueApp.UI
{
    public partial class frmNewDispatch : Form
    {
        private RescueContext db;

        public frmNewDispatch()
        {
            InitializeComponent();
        }

        public frmNewDispatch(RescueContext db)
        {
            InitializeComponent();
            this.db = db;
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            // 获取输入值
            int dispatchID;
            int rescueID;
            string driver = txt_driver.Text.Trim();
            string vehicleLicense = txt_vehicleLicense.Text.Trim();
            DateTime arrivedTime = dtp_arrivedTime.Value;
            string cause = txt_cause.Text.Trim();
            float cost;

            // 验证所有字段是否已填写
            if (string.IsNullOrEmpty(txt_dispatchID.Text) || 
                string.IsNullOrEmpty(txt_rescueID.Text) ||
                string.IsNullOrEmpty(driver) ||
                string.IsNullOrEmpty(vehicleLicense) ||
                string.IsNullOrEmpty(cause) ||
                string.IsNullOrEmpty(txt_cost.Text))
            {
                MessageBox.Show("请填写完整信息！");
                return;
            }

            // 验证派遣ID是否为整数
            if (!int.TryParse(txt_dispatchID.Text, out dispatchID))
            {
                MessageBox.Show("派遣ID必须为整数！");
                return;
            }

            // 验证救援申请ID是否为整数
            if (!int.TryParse(txt_rescueID.Text, out rescueID))
            {
                MessageBox.Show("救援申请ID必须为整数！");
                return;
            }

            // 验证救援申请ID是否存在
            var rescueApply = db.RescueApplies.Find(rescueID);
            if (rescueApply == null)
            {
                MessageBox.Show("救援申请ID不存在！");
                return;
            }

            // 验证救援费用是否为数字
            if (!float.TryParse(txt_cost.Text, out cost))
            {
                MessageBox.Show("救援费用必须为数字！");
                return;
            }

            // 创建插入派遣信息的SQL命令
            string sql = @"
        -- 启用标识列显式插入
            SET IDENTITY_INSERT RescueDispatch ON;
        INSERT INTO RescueDispatch (
            DispatchID,
            RescueID,
            DispatchDriver,
            DispatchVehicleLicense,
            ArrivalTime,
            MalfunctionCause,
            RescueCost
        ) VALUES (
            @DispatchID,
            @RescueID,
            @DispatchDriver,
            @DispatchVehicleLicense,
            @ArrivalTime,
            @MalfunctionCause,
            @RescueCost
        );
        -- 禁用标识列显式插入
        SET IDENTITY_INSERT RescueDispatch OFF";

            IDictionary<string, object> dict = new Dictionary<string, object>
            {
                { "@DispatchID", dispatchID },
                { "@RescueID", rescueID },
                { "@DispatchDriver", driver },
                { "@DispatchVehicleLicense", vehicleLicense },
                { "@ArrivalTime", arrivedTime },
                { "@MalfunctionCause", cause },
                { "@RescueCost", cost }
            };

            SqlParameter[] parameters = dict.ToSqlParameters();
            int affected = db.ExecuteSql(sql, parameters);

            if (affected > 0)
            {
                MessageBox.Show("派遣记录添加成功！");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("派遣记录添加失败！");
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}